﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Home.MobileSales.ImpExpData.InterfaceView;
using Home.MobileSales.ImpExpData.Presenter;

namespace Home.MobileSales.ImpExpData.View
{
    public partial class ImportForm : Form, IExportImportView
    {
        private bool _isImport;
       
        public ImportForm(bool isImport)
        {
            InitializeComponent();
            //hScrollBar1.Controls.Add(logListBox);
            new ExportImportPresenter(this, isImport);
        }

        private void loadButton_Click(object sender, EventArgs e)
        {
            if ((string)((Button)sender).Tag == "execute")
            {
                Execute(null, null);
                ((Button) sender).Tag = "abort";
                ((Button)sender).Text = "Перервати";
            }
            else if ((string)((Button)sender).Tag == "abort")
            {
                Abort(null, null);
                IsImport = _isImport;
            }
            ImportExportTabControl.SelectedIndex = 1;
        }

        private void allGoodsCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            remainingGoodsCheckBox.Checked = allGoodsCheckBox.Checked;
            remainingGoodsCheckBox.Enabled = !allGoodsCheckBox.Checked;
        }

        private void repeatRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            repeatVisistPanel.Visible = repeatRadioButton.Checked;
        }

        private void singleDataRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            singleDateVisitPanel.Visible = singleDataRadioButton.Checked;

        }

        private void exportVisitsCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            visitPanel.Visible = exportVisitsCheckBox.Checked;
        }

        private void buyersCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            debtCheckBox.Checked = buyersCheckBox.Checked;
            debtCheckBox.Enabled = !buyersCheckBox.Checked;
        }

        #region IExportImportView Members

        public bool IsAllGoods
        {
            get
            {
                return allGoodsCheckBox.Checked;
            }
            set
            {
                allGoodsCheckBox.Checked = value;
            }
        }

        public bool IsRemaining
        {
            get
            {
                return (remainingGoodsCheckBox.Checked && remainingGoodsCheckBox.Enabled);
            }
            set
            {
                remainingGoodsCheckBox.Checked = value;
            }
        }
        
        public bool IsBuyers
        {
            get
            {
                return buyersCheckBox.Checked;
            }
            set
            {
                buyersCheckBox.Checked = value;
            }
        }
        
        public bool IsDebts
        {
            get
            {
                return (debtCheckBox.Checked && debtCheckBox.Enabled);
            }
            set
            {
                debtCheckBox.Checked = value;
            }
        }

        public void EndData()
        {
            MessageBox.Show("Виконано");
            statusProgressBar.Value = 0;
            statusLabel.Text = string.Empty;
            IsImport = _isImport;
        }

        public void ErrorData()
        {
            MessageBox.Show("Загрузка перервана", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            IsImport = _isImport;
        }

        public void AddLogListBox(string logElement, bool er)
        {
            logListView.Items.Add(new ListViewItem(logElement) {ForeColor = er ? Color.Red : Color.Black});
        }

        public void SetStatusLabel(string caption)
        {
            statusLabel.Text = caption;
        }

        public void SetStatusPosition(int position)
        {
            statusProgressBar.Value = position;
        }

        public void SetStatusMax(int max)
        {
            statusProgressBar.Maximum = max;
        }

        public bool IsImport
        {
            get
            {
                return _isImport;
            }
            set
            {
                _isImport = value;
                if (_isImport)
                {
                    this.Text = "Завантаження даних";
                    ExecuteButton.Text = "Завантажити";
                    ExecuteButton.Tag = "execute";
                    importPanel.Visible = true;
                    exportPanel.Visible = false;

                }
                else
                {
                    this.Text = "Вигрузка даних";
                    ExecuteButton.Text = "Вигрузити";
                    ExecuteButton.Tag = "execute";
                    importPanel.Visible = false;
                    exportPanel.Visible = true;
                    beginDateTimePicker.Value = DateTime.Now;
                    endDateTimePicker.Value = DateTime.Now;
                    newDataRadioButton.Checked = true;
                    singleDateDateTimePicker.Value = DateTime.Now;
                    
                }
            }
        }

        public TypeExport GetTypeVisitExport
        {
            get
            {
                if (newDataRadioButton.Checked)
                    return TypeExport.OnlyNew;
                if (singleDataRadioButton.Checked)
                    return TypeExport.SingleDate;
                if (repeatRadioButton.Checked)
                {
                    return TypeExport.RepeatDate;
                }
                return TypeExport.OnlyNew;
            }
        }

        public bool IsVisits
        {
            get
            {
                return exportVisitsCheckBox.Checked;
            }
            set
            {
                exportVisitsCheckBox.Checked = value;
            }
        }

        public bool OnlyExecute
        {
            get { return onlyExecuteCheckBox.Checked; }
        }

        public DateTime BeginDate
        {
            get
            {
                return beginDateTimePicker.Value;
            }
            set
            {
                beginDateTimePicker.Value = value;
            }
        }

        public DateTime EndDate
        {
            get
            {
                return endDateTimePicker.Value;
            }
            set
            {
                endDateTimePicker.Value = value;
            }
        }

        public DateTime SingleDate
        {
            get
            {
                return singleDateDateTimePicker.Value;
            }
            set
            {
                singleDateDateTimePicker.Value = value;
            }
        }


        public event EventHandler<EventArgs> Execute;
        public event EventHandler<EventArgs> Abort;

        #endregion

    }
}